package com.ruoyi.project.modules.errorCodeCategory.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.project.modules.errorCodeCategory.domain.ErrorCodeCategory;
import com.ruoyi.project.modules.errorCodeCategory.service.IErrorCodeCategoryService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.domain.Ztree;

/**
 * 错误码分类Controller
 *
 * @author smallrain
 * @date 2022-04-04
 */
@Controller
@RequestMapping("/errorCodeCategory/errorCodeCategory")
public class ErrorCodeCategoryController extends BaseController {
    private String prefix = "errorCodeCategory/errorCodeCategory";

    @Autowired
    private IErrorCodeCategoryService errorCodeCategoryService;

    @RequiresPermissions("errorCodeCategory:errorCodeCategory:view")
    @GetMapping()
    public String errorCodeCategory() {
        return prefix + "/errorCodeCategory";
    }

    /**
     * 查询错误码分类树列表
     */
    //@RequiresPermissions("errorCodeCategory:errorCodeCategory:list")
    @PostMapping("/list")
    @ResponseBody
    public List<ErrorCodeCategory> list(ErrorCodeCategory errorCodeCategory) {
        List<ErrorCodeCategory> list = errorCodeCategoryService.selectErrorCodeCategoryList(errorCodeCategory);
        return list;
    }

    /**
     * 导出错误码分类列表
     */
    @RequiresPermissions("errorCodeCategory:errorCodeCategory:export")
    @Log(title = "错误码分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ErrorCodeCategory errorCodeCategory) {
        List<ErrorCodeCategory> list = errorCodeCategoryService.selectErrorCodeCategoryList(errorCodeCategory);
        ExcelUtil<ErrorCodeCategory> util = new ExcelUtil<ErrorCodeCategory>(ErrorCodeCategory.class);
        return util.exportExcel(list, "错误码分类数据");
    }

    /**
     * 新增错误码分类
     */
    @GetMapping(value = {"/add/{errorCodeCategoryId}", "/add/"})
    public String add(@PathVariable(value = "errorCodeCategoryId", required = false) Long errorCodeCategoryId, ModelMap mmap) {
        if (StringUtils.isNotNull(errorCodeCategoryId)) {
            mmap.put("errorCodeCategory", errorCodeCategoryService.selectErrorCodeCategoryByErrorCodeCategoryId(errorCodeCategoryId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存错误码分类
     */
    @RequiresPermissions("errorCodeCategory:errorCodeCategory:add")
    @Log(title = "错误码分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ErrorCodeCategory errorCodeCategory) {
        return toAjax(errorCodeCategoryService.insertErrorCodeCategory(errorCodeCategory));
    }

    /**
     * 修改错误码分类
     */
    @GetMapping("/edit/{errorCodeCategoryId}")
    public String edit(@PathVariable("errorCodeCategoryId") Long errorCodeCategoryId, ModelMap mmap) {
        ErrorCodeCategory errorCodeCategory = errorCodeCategoryService.selectErrorCodeCategoryByErrorCodeCategoryId(errorCodeCategoryId);
        mmap.put("errorCodeCategory", errorCodeCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存错误码分类
     */
    @RequiresPermissions("errorCodeCategory:errorCodeCategory:edit")
    @Log(title = "错误码分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ErrorCodeCategory errorCodeCategory) {
        return toAjax(errorCodeCategoryService.updateErrorCodeCategory(errorCodeCategory));
    }

    /**
     * 删除
     */
    @RequiresPermissions("errorCodeCategory:errorCodeCategory:remove")
    @Log(title = "错误码分类", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{errorCodeCategoryId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("errorCodeCategoryId") Long errorCodeCategoryId) {
        return toAjax(errorCodeCategoryService.deleteErrorCodeCategoryByErrorCodeCategoryId(errorCodeCategoryId));
    }

    /**
     * 选择错误码分类树
     */
    @GetMapping(value = {"/selectErrorCodeCategoryTree/{errorCodeCategoryId}", "/selectErrorCodeCategoryTree/"})
    public String selectErrorCodeCategoryTree(@PathVariable(value = "errorCodeCategoryId", required = false) Long errorCodeCategoryId, ModelMap mmap) {
        if (StringUtils.isNotNull(errorCodeCategoryId)) {
            mmap.put("errorCodeCategory", errorCodeCategoryService.selectErrorCodeCategoryByErrorCodeCategoryId(errorCodeCategoryId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载错误码分类树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = errorCodeCategoryService.selectErrorCodeCategoryTree();
        return ztrees;
    }
}
